/**
 * project: finance-manage
 * package: com.example.servlets
 * 用户请求处理类
 */

package com.example.servlets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.services.AccountService;
import com.example.util.Debug;

import java.io.IOException;
import java.io.PrintWriter;

public class AccountServlet extends HttpServlet implements com.example.interfaces.PostController {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 从请求中获取数据流并读取字符串
        String formData = req.getReader().readLine();
        Debug.print(getClass(), 1, ":", formData);
        // 实例化数据处理类 
        AccountService accountService = new AccountService(formData);
        // 调用服务层的方法进行处理
        String result = accountService.run();
        // 关闭数据处理类
        accountService.close();
        Debug.print(getClass(), 2, ":", result);

        //设置返回值类型
        resp.setContentType("application/json");
        //设置返回值编码
        resp.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        out.println(result);
        out.flush();
        out.close();
    }
}
